package com.dream.technology.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dream.technology.db.entity.RcField;
import com.dream.technology.db.entity.RcFieldQuery;
import com.dream.technology.db.entity.RcFieldQuery.Criteria;
import com.dream.technology.db.entity.RcFieldSelect;
import com.dream.technology.db.entity.RcFieldSelectQuery;
import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.service.RcFieldSelectService;
import com.dream.technology.service.RcFieldService;
import com.dream.technology.vo.ErrorCode;

import io.swagger.annotations.Api;

@Api("字段列表")
@RestController
@RequestMapping("/risk/admin/field")
public class RcFieldController {

    private static final Logger logger = LoggerFactory.getLogger("APMInfoDev");

    @Autowired
    private RcFieldService rcFieldService;
    @Autowired
    private RcFieldSelectService rcFieldSelectService;

    /**
     * 字段列表接口
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(){
        RcFieldQuery rcFieldQuery = new RcFieldQuery();
        List<RcField> rcFieldList = rcFieldService.selectByExample(rcFieldQuery);
        logger.info(JSON.toJSONString(rcFieldList));
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcFieldList);
    }
    /**
     * 字段列表接口
     * @return
     */
    @PostMapping("/costTime")
    public GenericRspDTO<Object> costTime(@RequestBody Map<String,Object> params) throws InterruptedException {
        RcFieldQuery rcFieldQuery = new RcFieldQuery();
//        List<RcField> rcFieldList = rcFieldService.selectByExample(rcFieldQuery);
//        logger.info(JSON.toJSONString(rcFieldList));
//        Thread.sleep(1000L);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), params);
    }

    /**
     *  添加字段
     * @param rcField
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> addField(RcField rcField){

        int insert = rcFieldService.insertSelective(rcField);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcField);

    }

    /**
     * 获取字段回显接口
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<Map<String, Object>> getFieldById(Long id){
    	Map<String, Object> map = new HashMap<String, Object>();
        RcField rcField = rcFieldService.selectByPrimaryKey(id);
        if (rcField != null) {
        	if (rcField.getDataType() == 3 || rcField.getDataType() == 4) {
        		// 此时是单选或者多选,需要查询下拉框的值
        		RcFieldSelectQuery query = new RcFieldSelectQuery();
                RcFieldSelectQuery.Criteria criteria = query.createCriteria();
                criteria.andFieldIdEqualTo(id);
                List<RcFieldSelect> rcFieldSelects = rcFieldSelectService.selectByExample(query);
                map.put("selectValues", rcFieldSelects);
        	}
        }
        map.put("rcField", rcField);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), map);
    }

    /**
     * 获取字段回显接口
     * @param id
     * @return
     */
    @GetMapping("/getFieldByCode")
    public GenericRspDTO<Map<String, Object>> getFieldByCode(String fieldCode){
    	Map<String, Object> map = new HashMap<String, Object>();
    	RcFieldQuery fieldQuery = new RcFieldQuery();
    	Criteria fieldCriteria = fieldQuery.createCriteria();
    	fieldCriteria.andFieldCodeEqualTo(fieldCode);
        List<RcField> rcFiledList = rcFieldService.selectByExample(fieldQuery);

        if (CollectionUtils.isNotEmpty(rcFiledList)) {
        	RcField rcField = rcFiledList.get(0);
        	if (rcField.getDataType() == 3 || rcField.getDataType() == 4) {
        		// 此时是单选或者多选,需要查询下拉框的值
        		RcFieldSelectQuery query = new RcFieldSelectQuery();
                RcFieldSelectQuery.Criteria criteria = query.createCriteria();
                criteria.andFieldIdEqualTo(rcField.getId());
                List<RcFieldSelect> rcFieldSelects = rcFieldSelectService.selectByExample(query);
                map.put("selectValues", rcFieldSelects);
        	}
        	map.put("rcField", rcField);
        }
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), map);
    }

    /**
     * 修改字段接口
     * @param rcField
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> updateField(RcField rcField){

        rcFieldService.updateByPrimaryKeySelective(rcField);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcField);
    }

    /**
     *  删除字段接口 (逻辑删除)
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public GenericRspDTO<Object> deleteField(Long id){


//        rcFieldService.deleteByPrimaryKey(id);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }
}
